﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThanhTu.GNC.Media.KQXS.DataAccess.BL;
using ThanhTu.GNC.Media.KQXS.DataAccess.DO;
using System.Data;


namespace GNCMedia.KQXS.MenuPage
{
    public partial class KetquaDudoan1capso : System.Web.UI.UserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            ProvinceBL vprovince = new ProvinceBL();
            PredictedBL vPredictedBL = new PredictedBL();
            PredictedDO PredictedDO = new PredictedDO();

            if (!IsPostBack)
            {
                try
                {
                    List<ProvinceDO> list_province = vprovince.selectadd();

                    ddl_dudoan_DB.DataSource = list_province;
                    ddl_dudoan_DB.DataTextField = "princeName";
                    ddl_dudoan_DB.DataValueField = "provinceID";
                    ddl_dudoan_DB.DataBind();
                    ddl_dudoan_BK.DataSource = list_province;
                    ddl_dudoan_BK.DataTextField = "princeName";
                    ddl_dudoan_BK.DataValueField = "provinceID";
                    ddl_dudoan_BK.DataBind();

                    Rpt_DB.DataSource = vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_BK.SelectedValue, "2");
                    Rpt_DB.DataBind();
                    Rpt_BK.DataSource = vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_BK.SelectedValue, "1");
                    Rpt_BK.DataBind();

                    lbl_tong_BK.Text = vPredictedBL.GetALLPredicted(DateTime.Now, ddl_dudoan_BK.SelectedValue, "1").Rows.Count.ToString();
                    lbl_Tong_dudoanDB.Text = vPredictedBL.GetALLPredicted(DateTime.Now, ddl_dudoan_DB.SelectedValue, "2").Rows.Count.ToString();
                    lbl_tinh_duodoan_DB.Text = ddl_dudoan_DB.SelectedItem.Text;
                    lbl_tinhdudoan_BK.Text = ddl_dudoan_BK.SelectedItem.Text;
                    lbl_provincename_DB.Text = ddl_dudoan_DB.SelectedItem.Text;
                    lbl_provincename_BK.Text = ddl_dudoan_BK.SelectedItem.Text;
                    int max_db = 0;
                    int max_bk = 0;
                    int min_db = 1000000;
                    int min_bk = 1000000;
                    string smax_db = string.Empty;
                    string smax_bk = string.Empty;
                    string smin_bk = string.Empty;
                    string smin_db = string.Empty;
                    foreach (DataRow row in vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_BK.SelectedValue, "1").Rows)
                    {
                        if (Convert.ToInt32(row["Count_number"].ToString()) >= max_bk)
                        {
                            max_bk = Convert.ToInt32(row["Count_number"].ToString());
                            smax_bk = row["NUMBER_PREDICTED"].ToString();
                        }
                        if (Convert.ToInt32(row["Count_number"].ToString()) <= min_bk)
                        {
                            min_bk = Convert.ToInt32(row["Count_number"].ToString());
                            smin_bk = row["NUMBER_PREDICTED"].ToString();
                        }
                    }
                    foreach (DataRow row in vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_DB.SelectedValue, "2").Rows)
                    {
                        if (Convert.ToInt32(row["Count_number"].ToString()) >= max_db)
                        {
                            max_db = Convert.ToInt32(row["Count_number"].ToString());
                            smax_db = row["NUMBER_PREDICTED"].ToString();
                        }
                        if (Convert.ToInt32(row["Count_number"].ToString()) <= min_db)
                        {
                            min_db = Convert.ToInt32(row["Count_number"].ToString());
                            smin_db = row["NUMBER_PREDICTED"].ToString();
                        }
                    }
                    lbl_dudoan_NN.Text = smax_db;
                    lbl_dudoan_NN_BK.Text = smax_bk;
                    lbl_dudoan_IN.Text = smin_db;
                    lbl_dudoan_IN_BK.Text = smin_bk;
                }
                catch (Exception ex)
                {
                    Response.Write("Error at du doan 1 cap so:" + ex);
                }
            }
        }

        protected void Ibt_duodoan_BK_Click(object sender, ImageClickEventArgs e)
        {
            PredictedBL vPredictedBL = new PredictedBL();
            PredictedDO PredictedDO = new PredictedDO();
            try
            {
                if (txt_dudoan_BK.Text.Trim().Length != 2)
                {
                    Response.Write(@"<script language='javascript'>alert('số bạn dự đoán phải có 2 chữ số');</script>");
                    return;
                }
                PredictedDO.Province_id = ddl_dudoan_BK.SelectedValue;
                PredictedDO.Number_predicted = txt_dudoan_BK.Text;
                PredictedDO.Predicted_date = DateTime.Now;
                PredictedDO.Stylepredicted_Id = 1;
                vPredictedBL.SavePredicted(PredictedDO);

                Rpt_BK.DataSource = vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_BK.SelectedValue, "1");
                Rpt_BK.DataBind();
                lbl_tong_BK.Text = vPredictedBL.GetALLPredicted(DateTime.Now, ddl_dudoan_BK.SelectedValue, "1").Rows.Count.ToString();
                lbl_tinhdudoan_BK.Text = ddl_dudoan_BK.SelectedItem.Text;
                lbl_provincename_BK.Text = ddl_dudoan_BK.SelectedItem.Text;


                int max_bk = 0;
                int min_bk = 1000000;
                string smax_bk = string.Empty;
                string smin_bk = string.Empty;
                foreach (DataRow row in vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_BK.SelectedValue, "1").Rows)
                {
                    if (Convert.ToInt32(row["Count_number"].ToString()) >= max_bk)
                    {
                        max_bk = Convert.ToInt32(row["Count_number"].ToString());
                        smax_bk = row["NUMBER_PREDICTED"].ToString();
                    }
                    if (Convert.ToInt32(row["Count_number"].ToString()) <= min_bk)
                    {
                        min_bk = Convert.ToInt32(row["Count_number"].ToString());
                        smin_bk = row["NUMBER_PREDICTED"].ToString();
                    }
                }

                lbl_dudoan_NN_BK.Text = smax_bk;
                lbl_dudoan_IN_BK.Text = smin_bk;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void Ibt_dudoan_DB_Click(object sender, ImageClickEventArgs e)
        {
            PredictedBL vPredictedBL = new PredictedBL();
            PredictedDO PredictedDO = new PredictedDO();
            try
            {
                if (txt_dudoan_DB.Text.Trim().Length != 2)
                {
                    Response.Write(@"<script language='javascript'>alert('số bạn dự đoán phải có 2 chữ số');</script>");
                    return;
                }
                PredictedDO.Province_id = ddl_dudoan_DB.SelectedValue;
                PredictedDO.Number_predicted = txt_dudoan_DB.Text;
                PredictedDO.Predicted_date = DateTime.Now;
                PredictedDO.Stylepredicted_Id = 2;
                vPredictedBL.SavePredicted(PredictedDO);
               
                Rpt_DB.DataSource = vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_DB.SelectedValue, "2");
                Rpt_DB.DataBind();
                lbl_Tong_dudoanDB.Text = vPredictedBL.GetALLPredicted(DateTime.Now, ddl_dudoan_DB.SelectedValue, "2").Rows.Count.ToString();
                lbl_tinh_duodoan_DB.Text = ddl_dudoan_DB.SelectedItem.Text;
                lbl_provincename_DB.Text = ddl_dudoan_DB.SelectedItem.Text;

                int max_db = 0;
                int min_db = 1000000;
                string smax_db = string.Empty;
                string smin_db = string.Empty;

                foreach (DataRow row in vPredictedBL.GetPredicted(DateTime.Now, ddl_dudoan_DB.SelectedValue, "2").Rows)
                {
                    if (Convert.ToInt32(row["Count_number"].ToString()) >= max_db)
                    {
                        max_db = Convert.ToInt32(row["Count_number"].ToString());
                        smax_db = row["NUMBER_PREDICTED"].ToString();
                    }
                    if (Convert.ToInt32(row["Count_number"].ToString()) <= min_db)
                    {
                        min_db = Convert.ToInt32(row["Count_number"].ToString());
                        smin_db = row["NUMBER_PREDICTED"].ToString();
                    }
                }
                lbl_dudoan_NN.Text = smax_db;
                lbl_dudoan_IN.Text = smin_db;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}